<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>穿梭框组件-基于layui</title>
    <link href="../../resources/js/plugins/layui/css/layui.css" type="text/css" rel="stylesheet"  />
    <script type="text/javascript" src="../../resources/js/plugins/layui/layui.js"></script>
    <style>
        .layui-table-tool{
            line-height: 50px;
            background: #EEF1F8;
            padding: 0px;
            color: #000000;
        }
        .layui-table-tool-temp{
            padding-right: 10px;
        }

        .layui-table-cell .layui-form-checkbox[lay-skin=primary] {
            top: 0px;
        }
    </style>
</head>
<body>
<div class="layui-container" style="margin-top: 15px;">

    <div id="jobsTransfer"></div>
    <div class="layui-row" style="text-align: center;margin-top: 20px">
        <div  >
            <button class="layui-btn blue-btn" id="btn_save" style="background-color: #1E85EC;color: #fff;">保存</button>
            <button class="layui-btn blue-btn" id="btn_canel" style="margin-left: 10px;background-color: #1E85EC;color: #fff;">取消</button>
        </div>
    </div>
    <div id="root1"></div>
</div>

</body>

<script>
    var dataList;
    layui.config({
        base: '../../resources/js/plugins/layui_exts/transfer/'
    }).use(['transfer'],function () {
        var transfer = layui.transfer,$ = layui.$;
        //数据源
        var data1 = [];
        var data2 = [];
        var ptype=GetQueryString('ptype');
        //表格列
        var cols = [{type: 'checkbox', fixed: 'left'}
            /*, {field: 'id', title: 'ID', width: 60, sort: false, hide: true}*/
            , {field: 'tempname', title: '模板名称', sort: false, minWidth: 80}
            , {field: 'jobname', title: '岗位名称', sort: false, minWidth: 80}
            , {field: 'zpcount', title: '招聘人数', minWidth: 80, hide: true}
            , {field: 'workcont', title: '工作内容', sort: false, hide: true, width: 90}
            , {field: 'workreq', title: '专业要求', minWidth: 100, hide: true}
            , {field: 'height', title: '身高', minWidth: 100, hide: true}
            , {field: 'gender', title: '身高', minWidth: 100, hide: true}
            , {field: 'vision', title: '视力', sort: false, hide: true}
            , {field: 'workyear', title: '工作年限', sort: false, edit: 'text', hide: true}
            , {field: 'agef', title: '年龄', sort: false, templet: '#jobsAgeTpl', hide: true}
            , {field: 'aget', title: '年龄', sort: false, hide: true, hide: true}
            , {field: 'educode', title: '学历编码', sort: false, hide: true}
            , {field: 'eduname', title: '学历名称', sort: false, hide: true}
            , {field: 'major', title: '所学专业', sort: false, hide: true}
            , {field: 'display', title: '是否显示', sort: false, hide: true}
            , {field: 'istemplete', title: '是否模版', sort: false, hide: true},
            ]
        //表格配置文件
        var tabConfig = {'page': true, 'limit': 100, 'limits': [10, 50, 100], 'height': 400};
     //   var tabConfig = {'page':false,'limits':[100,50,10],'height':400};
        /*var tb1= transfer.render({
            elem: "#jobsTransfer", //指定元素
            cols: cols, //表格列  支持layui数据表格所有配置
            url: '/ProjectJobTemplate/findByName?ptype='+ptype,
            data: [data1,data2], //[左表数据,右表数据[非必填]]
            tabConfig: tabConfig //表格配置项 支持layui数据表格所有配置
        })*/
        var tab1;
        $.ajax({
            url:'/ProjectJobTemplate/findByName?ptype='+ptype,
            dataType:'JSON',
            type:'POST',
            success:function (json) {
                data1=json.datas;
                dataList = data1;
                tb1 = transfer.render({
                 //   ,id:'jobsTransferCh'
                        /*  ,parseData: function(res){
                       /* return {
                              "value": res.id //数据值
                              ,"title": res.tempname //数据标题
                              ,"disabled": res.disabled  //是否禁用
                              ,"checked": res.checked //是否选中
                          }*/

                    elem: "#jobsTransfer", //指定元素
                    cols: cols, //表格列  支持layui数据表格所有配置
                    //url: 'test/datas/flows.json',
                    data: [data1,data2], //[左表数据,右表数据[非必填]]
                    tabConfig: tabConfig //表格配置项 支持layui数据表格所有配置
                })
            }
        });

        //transfer.get(参数1:初始化返回值,参数2:获取数据[all,left,right,l,r],参数:指定数据字段)
       /* //获取数据
        $('#btn_save').on('click',function () {
           // var data = transfer.get(tb1,'r');
            var data = transfer.getData('jobsTransferCh'); //获得右侧数据
            // layer.msg(JSON.stringify(data))
            var tabledatas= parent.layui.table.cache['jobsTable'];
            var hasData=false;
            var hasIndex=0;
            if(data.length>0){
                var reArr=[];
                for(var i=0;i<data.length;i++){
                    for(var j=0;j<data1.length;j++){
                        if(data1[j].id === data[i].value){
                            reArr.push(data1[j]);
                            break;
                        }
                    }
                }
                data=[];
                data = reArr.slice(0);

                layui.each(data,function (i,t) {
                    if(!!!t.istemplate){
                        t.istemplate='否';
                    }
                    layui.each(tabledatas,function (j,d) {
                        if(t.jobname==d.jobname){
                            hasData=true;
                            return;
                        }
                    });
                   // tabledatas.splice(i+1, 0, t);
                });
            }
            if(hasData){
               var findex= layer.confirm('列表中已存在相同招聘岗位!是否进行覆盖!',{icon:5,title:"提示"},function () {
                   layui.each(data,function (i,t) {
                       var needOver=false;

                       layui.each(tabledatas,function (j,d) {
                           if(t.jobname==d.jobname){
                               needOver=true;
                               tabledatas[j]=t;
                           }
                       });

                       if(!needOver){
                           tabledatas.push(t);
                       }
                   });
                   parent.layui.table.cache['jobsTable']=tabledatas;
                   parent.layui.table.reload('jobsTable',{data:tabledatas});
                   parent.layer.closeAll();
                },function () {
                    layer.close(findex);
                });
            }else{
                layui.each(data,function (i,t) {
                    tabledatas.push(t);
                });
                parent.layui.table.cache['jobsTable']=tabledatas;
                parent.layui.table.reload('jobsTable',{data:tabledatas});
                parent.layer.closeAll();
                //tabledatas.push(data);
            }

        });*/

        //获取数据
        $('#btn_save').on('click',function () {
            var data = transfer.get(tb1,'r');
            // layer.msg(JSON.stringify(data))
            var tabledatas= parent.layui.table.cache['jobsTable'];
            var hasData=false;
            var hasIndex=0;
            if(data.length>0){
                layui.each(data,function (i,t) {
                    layui.each(tabledatas,function (j,d) {
                        if(t.jobname===d.jobname){
                            hasData=true;
                            return;
                        }
                    });
                    // tabledatas.splice(i+1, 0, t);
                });
            }
            if(hasData){
                var findex= layer.confirm('列表中已存在相同招聘岗位!是否进行覆盖!',{icon:5,title:"提示"},function () {
                    layui.each(data,function (i,t) {
                        var needOver=false;
                        layui.each(tabledatas,function (j,d) {
                            if(t.jobname===d.jobname){
                                needOver=true;
                                tabledatas[j]=t;
                            }
                        });

                        if(!needOver){
                            tabledatas.push(t);
                        }
                    });
                    parent.layui.table.cache['jobsTable']=tabledatas;
                    parent.layui.table.reload('jobsTable',{data:tabledatas});
                    parent.layer.closeAll();
                },function () {
                    layer.close(findex);
                });
            }else{
                layui.each(data,function (i,t) {
                    tabledatas.push(t);
                });
                parent.layui.table.cache['jobsTable']=tabledatas;
                parent.layui.table.reload('jobsTable',{data:tabledatas});
                parent.layer.closeAll();
                //tabledatas.push(data);
            }
        });

        $('#btn_canel').on('click',function () {
            parent.layer.closeAll();
        });

        function GetQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;

        }
    })
</script>
</html>